<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="条盒名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入条盒名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="条盒条码" prop="barCode">
          <el-input
            v-model="queryParams.barCode"
            placeholder="请输入条盒条码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="二维码" prop="qrCode">
          <el-input
            v-model="queryParams.qrCode"
            placeholder="请输入二维码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="积分" prop="point">
          <el-input
            v-model="queryParams.point"
            placeholder="请输入积分"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="回收状态" prop="state">
          <el-select v-model="queryParams.state" clearable placeholder="请选择回收状态">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="queryParams.state"
            placeholder="请输入(0不回收，1回收中)"
            clearable
            @keyup.enter.native="handleQuery"
          /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['yh:details:add']"
          >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['yh:details:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['yh:details:remove']"
          >删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['yh:details:export']"
          >导出</el-button>
        </el-col>  -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="条盒图片" align="center">
          <template slot-scope="scope">
            <img v-if="scope.row.imgUrl" :src="baseUrl+ scope.row.imgUrl" class="avatar" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="条盒类型" align="center" prop="type" />
        <el-table-column label="条盒名称" align="center" prop="name" />
        <el-table-column label="条盒条码" align="center" prop="barCode" />
        <el-table-column label="条码图片" align="center">
          <template slot-scope="scope">
            <img  v-if="scope.row.barCodeUrl" :src="baseUrl+ scope.row.barCodeUrl" class="avatar" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
        <!-- <el-table-column label="二维码图片" align="center">
          <template slot-scope="scope">
            <img v-if="scope.row.qrCode" :src="baseUrl+ scope.row.qrCode" class="avatar" style="width: 100px;height: 100px;">
          </template>
        </el-table-column> -->
        <el-table-column label="积分" align="center" prop="point" />
        <el-table-column label="回收状态" align="center" prop="statusText" />
        <el-table-column label="更新时间" align="center" prop="updateTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['yh:details:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['yh:details:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改条盒信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="条盒名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入条盒名称" />
          </el-form-item>
          <el-form-item label="条盒类型" prop="type">
            <el-select v-model="form.typeId" clearable placeholder="请选择条盒类型">
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.type"
                :value="item.id">
              </el-option>
            </el-select>
            <!-- <el-input v-model="form.type" placeholder="请输入条盒名称" /> -->
          </el-form-item>
          <el-form-item label="条盒图片">
            <el-upload
              class="avatar-uploader"
              :action="`${base}/common/upload`" 
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img  v-if="form.imgUrl" :src="baseUrl+form.imgUrl" class="avatar" style="width: 100px;height: 100px;">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
  
          <el-form-item label="条盒条码" prop="barCode">
            <el-input v-model="form.barCode" placeholder="请输入条盒条码" />
          </el-form-item>
          <el-form-item label="条码图片">
            <el-upload
              class="avatar-uploader"
              :action="`${base}/common/upload`"
              :show-file-list="false"
              :on-success="handlecodeSuccess"
              :before-upload="beforeAvatarUpload">
              <img  v-if="form.barCodeUrl" :src="baseUrl+form.barCodeUrl" class="avatar"  style="width: 100px;height: 100px;">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- <el-form-item label="二维码">
            <el-upload
              class="avatar-uploader"
              :action="`${base}/common/upload`"
              :show-file-list="false"
              :on-success="handletwoSuccess"
              :before-upload="beforeAvatarUpload">
              <img  v-if="form.qrCode" :src="baseUrl+form.qrCode" class="avatar"  style="width: 100px;height: 100px;">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item> -->
          <el-form-item label="扫码二维码" prop="qrCode">
            <el-switch
              v-model="form.qrCode"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item label="填写防伪码" prop="antiFakeCode">
            <el-switch
              v-model="form.antiFakeCode"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item label="积分" prop="point">
            <el-input v-model="form.point" placeholder="请输入积分" />
          </el-form-item>
  
          <el-form-item label="回收状态" prop="state">
              <el-switch
                v-model="form.state"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
          </el-form-item>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listDetails, getDetails, delDetails, addDetails, updateDetails,getAllTypeList } from "@/api/yh/details";
  import baseUrl from "@/api/baseUrl";
  export default {
    name: "Details",
    data() {
      return {
        baseUrl:baseUrl,
        base: process.env.VUE_APP_BASE_API,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 条盒信息表格数据
        detailsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          barCode: null,
          qrCode: null,
          point: null,
          antiFakeCode:null,
          state: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          // state: [
          //   { required: true, message: "(0不回收，1回收中)不能为空", trigger: "blur" }
          // ],
        },
        stateOptions:[
          {
            value:0,
            label:'不回收'
          },
          {
            value:1,
            label:'回收中'
          }
        ],
        typeOptions:[]
      };
    },
    created() {
      this.getList();
      this.getAlltype()
    },
    methods: {
        handletwoSuccess(res, file) {
            this.form.qrCode =  res.fileName 
          },
        handlecodeSuccess(res, file) {
            this.form.barCodeUrl =  res.fileName 
          },
        handleAvatarSuccess(res, file) {
          this.form.imgUrl =  res.fileName 
        },
        beforeAvatarUpload(file) {
          // const isJPG = file.type === 'image/jpeg';
          const isLt2M = file.size / 1024 / 1024 < 10;
  
          // if (!isJPG) {
          //   this.$message.error('上传头像图片只能是 JPG 格式!');
          // }
          if (!isLt2M) {
            this.$message.error('上传图片大小不能超过 10MB!');
          }
          return isLt2M;
        },
      // 查询所有条盒类型
      getAlltype(){
        this.loading = true;
        getAllTypeList({
          yhType : '2'
        }
        ).then(response => {
          this.typeOptions = response.data
          this.loading = false;
        });
      },
      /** 查询条盒信息列表 */
      getList() {
        this.loading = true;
        let arr = ['不回收','回收中']
        this.queryParams.yhType = '2'
        listDetails(this.queryParams).then(response => {
          this.detailsList = response.rows.map(item => {
            return {
              ...item,
              statusText:arr[item.state],
            }
          });
          console.log(this.detailsList,'this.detailsList')
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          typeId: null,
          name: null,
          barCode: null,
          qrCode: null,
          point: null,
          state: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加条盒信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDetails(id).then(response => {
          this.form = response.data;
          this.form.type = row.type
          // console.log(this.form,'this.form',row)
          this.open = true;
          this.title = "修改条盒信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if(this.form.antiFakeCode == 0 && this.form.qrCode == 0) return  this.$modal.msgError("请开启二维码或防伪码");
            console.log(this.form,'this.dorm')
            if (this.form.id != null) {
              updateDetails(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.yhType = '2'
              addDetails(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除条盒信息？').then(function() {
          return delDetails(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('yh/details/export', {
          ...this.queryParams
        }, `details_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  